package com.borqsprocessmanager.fragments;

import java.util.ArrayList;
import java.util.HashMap;

import android.database.Cursor;
import android.os.Bundle;
import android.view.LayoutInflater;
import android.view.View;
import android.view.ViewGroup;
import android.widget.ListView;

import com.borqsprocessmanager.App;
import com.borqsprocessmanager.IService;
import com.borqsprocessmanager.R;
import com.borqsprocessmanager.adapter.WhiteAdapter;
import com.borqsprocessmanager.db.DBProvider.WhiteListTable;
import com.borqsprocessmanager.info.ProcessStatsInfo;
import com.borqsprocessmanager.info.ReceiverProcessInfo;
import com.borqsprocessmanager.util.Log;

public class WhiteFragment extends BaseFragment {
    private final String TAG = "WhiteFragment";

    private View layout;
    private ListView mListView;

    private ArrayList<ProcessStatsInfo> whiteList;

    @Override
    public void onCreate(Bundle savedInstanceState) {
        // TODO Auto-generated method stub
        Log.v(TAG, "onCreate");
        super.onCreate(savedInstanceState);
        initData();
    }

    @Override
    public View onCreateView(LayoutInflater inflater, ViewGroup container,
            Bundle savedInstanceState) {
        layout = inflater.inflate(R.layout.res_fragment, null);
        findView(layout);
        return layout;
    }

    @Override
    public void onActivityCreated(Bundle savedInstanceState) {
        super.onActivityCreated(savedInstanceState);
        addHeader(mListView);
        setAdapter(mListView);
    }

    private void initData() {
        getWhiteListFromDB();
    }

    private void findView(View parentView) {
        mListView = (ListView) parentView.findViewById(R.id.main_list);
    }

    private void addHeader(ListView list) {
        LayoutInflater inflater = LayoutInflater.from(mActivity);
        View mHeader = inflater.inflate(R.layout.white_list_head, null);
        list.addHeaderView(mHeader);
    }

    private void getWhiteListFromDB() {
        Cursor cursor = App.mContext.getContentResolver().query(
                WhiteListTable.CONTENT_URI, null, null, null, null);
        Log.v(TAG, "White_cursor===" + cursor.getCount());
        if (null != cursor && cursor.getCount() > 0) {
            whiteList = new ArrayList<ProcessStatsInfo>();
            for (cursor.moveToFirst(); !cursor.isAfterLast(); cursor
                    .moveToNext()) {
                ProcessStatsInfo info = new ProcessStatsInfo();
                info.setAppName(cursor.getString(cursor
                        .getColumnIndex(WhiteListTable.APP_NAME)));
                info.setPackageName(cursor.getString(cursor
                        .getColumnIndex(WhiteListTable.PKG_NAME)));
                info.setProcessName(cursor.getString(cursor
                        .getColumnIndex(WhiteListTable.PROCESS_NAME)));
                whiteList.add(info);
                Log.v(TAG, "key===" + info.processName);
                Log.v(TAG, "appName===" + info.appName);
            }
        }
        if (cursor != null) {
            cursor.close();
        }
    }

    private void setAdapter(ListView listView) {
        WhiteAdapter itemAdapter = new WhiteAdapter(mActivity, whiteList);
        listView.setAdapter(itemAdapter);
        setListViewListener(listView, itemAdapter);
    }

    @Override
    protected void dismisListViewItem(int position) {
        removeWhiteListFromDB(whiteList.get(position).packageName);
        Log.v(TAG, "dismisListViewItem == " + whiteList.size());
        if (mWhiteMap.size() == 0) {
            Log.v(TAG, "whiteList.size() == 0");
            mActivity.onBackPressed();
        }
    }

    private void removeWhiteListFromDB(String packagename) {
        App.mContext.getContentResolver().delete(WhiteListTable.CONTENT_URI,
                WhiteListTable.PKG_NAME + "=?", new String[] { packagename });
        mWhiteMap.remove(packagename);
    }

    @Override
    public void onBindServiced(IService personService,
            HashMap<String, ReceiverProcessInfo> actionMap,
            HashMap<String, ProcessStatsInfo> whiteMap) {
        super.onBindServiced(personService, actionMap, whiteMap);
    }
}
